﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Web.Mvc;

using ProjectBase.Core;
using ProjectBase.Core.Web;

using Survey.Common;
using Survey.Entity.Services;

namespace Survey.Filters
{
    public static class Extensions
    {
        #region security
        public static bool IsActionProhibited(this IWebController<IViewModel> controller, string actionName,
            string controllerName)
        {
            var grantedController = SecurityManager.GrantedControllers
                .Where(c => c.ControllerName.IsEqual(controllerName))
                .FirstOrDefault();

            if (!controllerName.Is())
            {
                return true;
            }
            return Global.Settings.UseCurrentRole
                       ? grantedController.IsProhibitedForCurrentRole(actionName)
                       : grantedController.IsProhibitedForAnyRole(actionName);
        }

        static bool IsProhibitedForAnyRole(this IControllerAccess grantedController, string actionName)
        {
            if (grantedController.IsNull()
                || actionName.IsEmpty())
            {
                return true;
            }
            if (grantedController.Roles
                .Where(r => !r.UseDenied)
                .Any(role => role.GrantedActions
                                 .Contains(actionName, StringComparer.OrdinalIgnoreCase)))
            {
                return false;
            }
            if (grantedController.Roles
                .Where(r => r.UseDenied)
                .Any(role => !role.DeniedActions
                                  .Contains(actionName, StringComparer.OrdinalIgnoreCase)))
            {
                return false;
            }
            return true;
        }

        static bool IsProhibitedForCurrentRole(this IControllerAccess grantedController, string actionName)
        {
            if (grantedController.IsNull()
                || actionName.IsEmpty())
            {
                return true;
            }

            var role = grantedController.Roles
                .FirstOrDefault(
                    r =>
                    r.RoleName.Equals(SecurityManager.User.CurrentRole,
                        StringComparison.OrdinalIgnoreCase));
            if (role.UseDenied)
            {
                if (role.DeniedActions.Contains(actionName, StringComparer.OrdinalIgnoreCase))
                {
                    return true;
                }
            }
            else
            {
                if (!role.GrantedActions.Contains(actionName, StringComparer.OrdinalIgnoreCase))
                {
                    return true;
                }
            }
            return false;
        }
        #endregion security

        #region MasterPage
        public static bool HasResultMasterPage(this ActionResult actionResult)
        {
            if (actionResult.Is())
            {
                var emptyResult = actionResult as EmptyResult;
                if (emptyResult.Is())
                {
                    return false;
                }
                var partialResult = actionResult as PartialViewResult;
                if (partialResult.Is())
                {
                    return false;
                }
                var viewResult = actionResult as ViewResult;
                if (viewResult.Is())
                {
                    if (viewResult.View is IMasterPageLessView)
                    {
                        return false;
                    }
                    var name = viewResult.ViewName;
                    if (name.StartsWith(Constants.WebControls.ControlsPath)
                      || name.StartsWith(Constants.WebControls.ControlsPath2))
                    {
                        return false;
                    }
                    if (!name.EndsWith(".ascx", StringComparison.OrdinalIgnoreCase))
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        public static bool CurrentViewIs(this Common.Models.ILayoutModel model, string viewName)
        {
            return model.MasterModel.CurrentMasterPageValue.IsNotEmpty()
                && model.MasterModel.CurrentMasterPageValue.Equals(viewName, StringComparison.OrdinalIgnoreCase);
        }
        #endregion MasterPage
    }
}